<template>
  <div class="login-wrap">
    <div class="login-root">
      <div class="login-main">
        <img class="login-one-ball"
          src="https://assets.codehub.cn/micro-frontend/login/fca1d5960ccf0dfc8e32719d8a1d80d2.png" />
        <img class="login-two-ball"
          src="https://assets.codehub.cn/micro-frontend/login/4bcf705dad662b33a4fc24aaa67f6234.png" />
        <div class="login-container">
          <div class="login-side">
            <div class="login-bg-title">
              <h1>小米有品</h1>

              <h3 style="margin: 20px auto">
                小 米 有 品 商 城 管 理 端
              </h3>
            </div>
          </div>
          <div class="login-ID">
            <div class="login-header">
              <div class="login-title">欢迎回来</div>
              <div class="login-subtitle">请登录您的账户</div>
            </div>
            <div class="login-form-container">
              <lay-form-item :label-width="0">
                <lay-input :allow-clear="true" prefix-icon="layui-icon-username" placeholder="请输入用户名"
                  v-model="loginForm.account" size="lg"></lay-input>
              </lay-form-item>
              <lay-form-item :label-width="0">
                <lay-input :allow-clear="true" prefix-icon="layui-icon-password" placeholder="请输入密码" password
                  type="password" v-model="loginForm.password" size="lg"></lay-input>
              </lay-form-item>
              <div class="login-options">
                <lay-checkbox value="" name="like" v-model="remember" skin="primary" label="1">记住密码</lay-checkbox>
              </div>
              <lay-form-item :label-width="0">
                <lay-button class="login-button" type="primary" :loading="loging" :fluid="true"
                  loadingIcon="layui-icon-loading" @click="loginSubmit" size="lg">登录</lay-button>
              </lay-form-item>
            </div>

          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { login } from '../../api/module/user'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../../store/user'
import { layer } from '@layui/layer-vue'

export default defineComponent({
  setup() {
    const router = useRouter()
    const userStore = useUserStore()
    const loging = ref(false);
    const remember = ref(false)
    const loginForm = reactive({
      account: '',
      password: ''
    })

    const loginSubmit = async () => {
      loging.value = true;
      login(loginForm).then(({ data, code, msg }) => {
        setTimeout(() => {
          loging.value = false;
          if (code == 200) {
            layer.msg(msg, { icon: 1 }, async () => {
              userStore.token = data.token
              await userStore.loadMenus()
              await userStore.loadPermissions()
              router.push('/')
            })
          } else {
            layer.msg(msg, { icon: 2 })
          }
        }, 1000)
      })
    }

    return {
      loginSubmit,
      loginForm,
      remember,
      loging
    }
  }
})
</script>

<style scoped>
.login-captach {
  display: inline-block;
  vertical-align: bottom;
  width: 108px;
  height: 40px;
  color: var(--global-primary-color);
  margin-left: 8px;
  border-radius: 4px;
  border: 1px solid hsla(0, 0%, 60%, 0.46);
  transition: border 0.2s;
  box-sizing: border-box;
  background: #fff;
  overflow: hidden;
  cursor: pointer;
}

.login-one-ball {
  opacity: 0.4;
  position: absolute;
  max-width: 568px;
  left: -400px;
  bottom: 0px;
}

.login-two-ball {
  opacity: 0.4;
  position: absolute;
  max-width: 320px;
  right: -200px;
  top: -60px;
}

.login-wrap {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  overflow: auto;
  min-width: 600px;
  z-index: 9;
  background: #f5f7fa;
  background-size: cover;
  min-height: 100vh;
}

.login-wrap :deep(.layui-input-block) {
  margin-left: 0 !important;
}

.login-root {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  width: 100%;
  min-width: 320px;
  background-color: initial;
}

.login-main {
  position: relative;
  display: block;
}

.logo-container {
  max-width: calc(100vw - 28px);
  margin-bottom: 40px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-container .logo {
  display: inline-block;
  height: 30px;
  width: 143px;
  background: url() no-repeat 50%;
  background-size: contain;
  cursor: pointer;
}

.login-container {
  position: relative;
  overflow: hidden;
  width: 940px;
  height: 540px;
  max-width: calc(100vw - 28px);
  border-radius: 4px;
  background: #ffffff;
  display: flex;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border-radius: 16px;
}

.login-side {
  padding: 40px 20px 20px;
  background: #fff;
  flex: 1;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-bg-title {
  color: #333;
  text-align: center;
  padding: 20px;
}

.login-bg-title h1 {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 16px;
  color: #1890ff;
}

.login-bg-title h3 {
  font-size: 18px;
  font-weight: 400;
  color: #666;
  line-height: 1.6;
}

.login-ID {
  padding: 40px 50px;
  min-width: 420px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.login-subtitle {
  font-size: 16px;
  color: #666;
  font-weight: 400;
}

.login-form-container {
  width: 100%;
}

.login-options {
  margin: 15px 0;
  display: flex;
  justify-content: flex-start;
}

.login-button {
  margin-top: 25px !important;
  height: 48px !important;
  font-size: 16px !important;
  font-weight: 500 !important;
  border-radius: 8px !important;
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%) !important;
  border: none !important;
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3) !important;
  transition: all 0.3s ease !important;
}

.login-button:hover {
  transform: translateY(-2px) !important;
  box-shadow: 0 6px 20px rgba(24, 144, 255, 0.4) !important;
}

.login-button:active {
  transform: translateY(0) !important;
}

.login-container .layui-tab-head {
  background: transparent;
}

.login-container .layui-input-wrapper {
  margin-top: 10px;
  margin-bottom: 10px;
}

.login-container .layui-input-wrapper {
  margin-top: 12px;
  margin-bottom: 12px;
}

.login-container .assist {
  margin-top: 5px;
  margin-bottom: 5px;
  letter-spacing: 2px;
}

.login-container .layui-btn {
  margin: 10px 0px 10px 0px;
  letter-spacing: 2px;
  height: 40px;
}

.login-container .layui-line-horizontal {
  letter-spacing: 2px;
  margin-bottom: 34px;
  margin-top: 24px;
}

.other-ways {
  display: flex;
  justify-content: space-between;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 14px;
  font-weight: 400;
}

.other-ways li {
  width: 100%;
}

.line-container {
  justify-content: center;
  align-items: center;
  text-align: center;
  cursor: pointer;
}

.line-container .icon {
  height: 28px;
  width: 28px;
  margin-right: 0;
  vertical-align: middle;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px 0 rgb(9 30 66 / 4%), 0 1px 4px 0 rgb(9 30 66 / 10%),
    0 0 1px 0 rgb(9 30 66 / 10%);
}

.line-container .text {
  display: block;
  margin: 12px 0 0;
  font-size: 12px;
  color: #8592a6;
}

:deep(.layui-tab-title .layui-this) {
  background-color: transparent;
}
</style>
